{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据科学处理\n",
    "import pandas as pd  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "data = pd.read_csv('车贷违约预测.csv',encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "1  519488  56759  65325    89.55   61  22778  86  1967  2018   6  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "3  648134  72317  99750    73.68   76  17242  48  1995  2018   8  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 199717 entries, 0 to 199716\n",
      "Data columns (total 49 columns):\n",
      "客户编号              199717 non-null int64\n",
      "已发货款              199717 non-null int64\n",
      "资产成本              199717 non-null int64\n",
      "贷款与资产比列           199717 non-null float64\n",
      "品牌                199717 non-null int64\n",
      "骑车销售商             199717 non-null int64\n",
      "车厂                199717 non-null int64\n",
      "出生日期              199717 non-null int64\n",
      "货款日期              199717 non-null int64\n",
      "地区                199717 non-null int64\n",
      "对接员工编号            199717 non-null int64\n",
      "是否填写手机号           199717 non-null int64\n",
      "受否填写身份证           199717 non-null int64\n",
      "是否出具驾驶证           199717 non-null int64\n",
      "是否填写护照            199717 non-null int64\n",
      "信用评分              199717 non-null int64\n",
      "主账户贷款次数           199717 non-null int64\n",
      "主账户有效贷款次数         199717 non-null int64\n",
      "主账户中尚未还清有效贷款      199717 non-null int64\n",
      "主账户中已批准的贷款        199717 non-null int64\n",
      "主账户中已发放贷款         199717 non-null int64\n",
      "次账户贷款次数           199717 non-null int64\n",
      "次账户有效贷款次数         199717 non-null int64\n",
      "次账户中尚未还清有效贷款      199717 non-null int64\n",
      "次账户中已批准贷款         199717 non-null int64\n",
      "次账户中已发放贷款         199717 non-null int64\n",
      "主账户每月还款           199717 non-null int64\n",
      "次账户没用还款           199717 non-null int64\n",
      "近六个月新贷款次数         199717 non-null int64\n",
      "近六个月违约次数          199717 non-null int64\n",
      "平均贷款期限            199717 non-null int64\n",
      "第一次贷款距今时间         199717 non-null int64\n",
      "贷款查询次数            199717 non-null int64\n",
      "是否违约              199717 non-null int64\n",
      "贷款与资产比            199717 non-null float64\n",
      "贷款总次数             199717 non-null int64\n",
      "主账户无效贷款次数         199717 non-null int64\n",
      "次账户无效贷款次数         199717 non-null int64\n",
      "无效贷款总次数           199717 non-null int64\n",
      "尚未还清有效贷款总额        199717 non-null int64\n",
      "已批准贷款总额           199717 non-null int64\n",
      "已发放贷款总额           199717 non-null int64\n",
      "每月还款总额            199717 non-null int64\n",
      "贷款与已还贷款比列         199717 non-null float64\n",
      "主账户还款期数           199717 non-null int64\n",
      "次账户还款期数           199717 non-null int64\n",
      "贷款与已批准贷款比列        199717 non-null float64\n",
      "总贷款次数与总有效贷款次数比    199717 non-null float64\n",
      "工作类型              199717 non-null int64\n",
      "dtypes: float64(5), int64(44)\n",
      "memory usage: 74.7 MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.贷款与资产比列 = data.贷款与资产比列.astype('str')\n",
    "data.贷款与资产比 = data.贷款与资产比.astype('str')\n",
    "data.贷款与已还贷款比列 = data.贷款与已还贷款比列.astype('str')\n",
    "data.贷款与已批准贷款比列 = data.贷款与已批准贷款比列.astype('str')\n",
    "data.总贷款次数与总有效贷款次数比 = data.总贷款次数与总有效贷款次数比.astype('str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.00</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>535690.886665</td>\n",
       "      <td>54256.272280</td>\n",
       "      <td>7.582391e+04</td>\n",
       "      <td>74.643960</td>\n",
       "      <td>72.698508</td>\n",
       "      <td>19634.049665</td>\n",
       "      <td>69.085766</td>\n",
       "      <td>1983.876921</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>7.245222</td>\n",
       "      <td>...</td>\n",
       "      <td>1.743125e+05</td>\n",
       "      <td>2.299233e+05</td>\n",
       "      <td>2.294165e+05</td>\n",
       "      <td>1.344553e+04</td>\n",
       "      <td>inf</td>\n",
       "      <td>5.059582e+04</td>\n",
       "      <td>2.928000e+03</td>\n",
       "      <td>5.535709e+02</td>\n",
       "      <td>1.438913</td>\n",
       "      <td>0.487475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>68193.411418</td>\n",
       "      <td>12977.656996</td>\n",
       "      <td>1.892894e+04</td>\n",
       "      <td>11.490485</td>\n",
       "      <td>69.706185</td>\n",
       "      <td>3493.655400</td>\n",
       "      <td>22.128288</td>\n",
       "      <td>9.805565</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.481338</td>\n",
       "      <td>...</td>\n",
       "      <td>9.813640e+05</td>\n",
       "      <td>2.530977e+06</td>\n",
       "      <td>2.534185e+06</td>\n",
       "      <td>1.531618e+05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.275670e+06</td>\n",
       "      <td>1.065410e+05</td>\n",
       "      <td>1.141343e+05</td>\n",
       "      <td>0.792213</td>\n",
       "      <td>0.561915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>417428.000000</td>\n",
       "      <td>13320.000000</td>\n",
       "      <td>3.700000e+04</td>\n",
       "      <td>10.030000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10524.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>1949.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-6.678296e+06</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>-110000.33</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>476762.000000</td>\n",
       "      <td>46977.000000</td>\n",
       "      <td>6.571400e+04</td>\n",
       "      <td>68.730000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>16505.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>1977.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>535571.000000</td>\n",
       "      <td>53703.000000</td>\n",
       "      <td>7.092200e+04</td>\n",
       "      <td>76.670000</td>\n",
       "      <td>61.000000</td>\n",
       "      <td>20333.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1986.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>594571.000000</td>\n",
       "      <td>60247.000000</td>\n",
       "      <td>7.915900e+04</td>\n",
       "      <td>83.590000</td>\n",
       "      <td>130.000000</td>\n",
       "      <td>23000.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1992.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>3.818900e+04</td>\n",
       "      <td>6.720600e+04</td>\n",
       "      <td>6.508500e+04</td>\n",
       "      <td>2.094000e+03</td>\n",
       "      <td>1.26</td>\n",
       "      <td>2.500000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.670000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>671084.000000</td>\n",
       "      <td>990572.000000</td>\n",
       "      <td>1.628992e+06</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>261.000000</td>\n",
       "      <td>24803.000000</td>\n",
       "      <td>156.000000</td>\n",
       "      <td>2000.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>22.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>9.652492e+07</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>2.564281e+07</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.980000e+07</td>\n",
       "      <td>5.000000e+07</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                客户编号           已发货款          资产成本        贷款与资产比列  \\\n",
       "count  199717.000000  199717.000000  1.997170e+05  199717.000000   \n",
       "mean   535690.886665   54256.272280  7.582391e+04      74.643960   \n",
       "std     68193.411418   12977.656996  1.892894e+04      11.490485   \n",
       "min    417428.000000   13320.000000  3.700000e+04      10.030000   \n",
       "25%    476762.000000   46977.000000  6.571400e+04      68.730000   \n",
       "50%    535571.000000   53703.000000  7.092200e+04      76.670000   \n",
       "75%    594571.000000   60247.000000  7.915900e+04      83.590000   \n",
       "max    671084.000000  990572.000000  1.628992e+06      95.000000   \n",
       "\n",
       "                  品牌          骑车销售商             车厂           出生日期      货款日期  \\\n",
       "count  199717.000000  199717.000000  199717.000000  199717.000000  199717.0   \n",
       "mean       72.698508   19634.049665      69.085766    1983.876921    2018.0   \n",
       "std        69.706185    3493.655400      22.128288       9.805565       0.0   \n",
       "min         1.000000   10524.000000      45.000000    1949.000000    2018.0   \n",
       "25%        14.000000   16505.000000      48.000000    1977.000000    2018.0   \n",
       "50%        61.000000   20333.000000      86.000000    1986.000000    2018.0   \n",
       "75%       130.000000   23000.000000      86.000000    1992.000000    2018.0   \n",
       "max       261.000000   24803.000000     156.000000    2000.000000    2018.0   \n",
       "\n",
       "                  地区  ...    尚未还清有效贷款总额       已批准贷款总额       已发放贷款总额  \\\n",
       "count  199717.000000  ...  1.997170e+05  1.997170e+05  1.997170e+05   \n",
       "mean        7.245222  ...  1.743125e+05  2.299233e+05  2.294165e+05   \n",
       "std         4.481338  ...  9.813640e+05  2.530977e+06  2.534185e+06   \n",
       "min         1.000000  ... -6.678296e+06  0.000000e+00  0.000000e+00   \n",
       "25%         4.000000  ...  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "50%         6.000000  ...  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "75%        10.000000  ...  3.818900e+04  6.720600e+04  6.508500e+04   \n",
       "max        22.000000  ...  9.652492e+07  1.000000e+09  1.000000e+09   \n",
       "\n",
       "             每月还款总额  贷款与已还贷款比列       主账户还款期数       次账户还款期数    贷款与已批准贷款比列  \\\n",
       "count  1.997170e+05  199717.00  1.997170e+05  1.997170e+05  1.997170e+05   \n",
       "mean   1.344553e+04        inf  5.059582e+04  2.928000e+03  5.535709e+02   \n",
       "std    1.531618e+05        NaN  2.275670e+06  1.065410e+05  1.141343e+05   \n",
       "min    0.000000e+00 -110000.33  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "25%    0.000000e+00       1.00  0.000000e+00  0.000000e+00  1.000000e+00   \n",
       "50%    0.000000e+00       1.00  0.000000e+00  0.000000e+00  1.000000e+00   \n",
       "75%    2.094000e+03       1.26  2.500000e+01  0.000000e+00  1.000000e+00   \n",
       "max    2.564281e+07        inf  1.000000e+09  1.980000e+07  5.000000e+07   \n",
       "\n",
       "       总贷款次数与总有效贷款次数比           工作类型  \n",
       "count   199717.000000  199717.000000  \n",
       "mean         1.438913       0.487475  \n",
       "std          0.792213       0.561915  \n",
       "min          1.000000       0.000000  \n",
       "25%          1.000000       0.000000  \n",
       "50%          1.000000       0.000000  \n",
       "75%          1.670000       1.000000  \n",
       "max         18.000000       2.000000  \n",
       "\n",
       "[8 rows x 49 columns]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 异常值处理\n",
    "# 没有看出异常值\n",
    "data['客户编号'].duplicated().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              0\n",
       "已发货款              0\n",
       "资产成本              0\n",
       "贷款与资产比列           0\n",
       "品牌                0\n",
       "骑车销售商             0\n",
       "车厂                0\n",
       "出生日期              0\n",
       "货款日期              0\n",
       "地区                0\n",
       "对接员工编号            0\n",
       "是否填写手机号           0\n",
       "受否填写身份证           0\n",
       "是否出具驾驶证           0\n",
       "是否填写护照            0\n",
       "信用评分              0\n",
       "主账户贷款次数           0\n",
       "主账户有效贷款次数         0\n",
       "主账户中尚未还清有效贷款      0\n",
       "主账户中已批准的贷款        0\n",
       "主账户中已发放贷款         0\n",
       "次账户贷款次数           0\n",
       "次账户有效贷款次数         0\n",
       "次账户中尚未还清有效贷款      0\n",
       "次账户中已批准贷款         0\n",
       "次账户中已发放贷款         0\n",
       "主账户每月还款           0\n",
       "次账户没用还款           0\n",
       "近六个月新贷款次数         0\n",
       "近六个月违约次数          0\n",
       "平均贷款期限            0\n",
       "第一次贷款距今时间         0\n",
       "贷款查询次数            0\n",
       "是否违约              0\n",
       "贷款与资产比            0\n",
       "贷款总次数             0\n",
       "主账户无效贷款次数         0\n",
       "次账户无效贷款次数         0\n",
       "无效贷款总次数           0\n",
       "尚未还清有效贷款总额        0\n",
       "已批准贷款总额           0\n",
       "已发放贷款总额           0\n",
       "每月还款总额            0\n",
       "贷款与已还贷款比列         0\n",
       "主账户还款期数           0\n",
       "次账户还款期数           0\n",
       "贷款与已批准贷款比列        0\n",
       "总贷款次数与总有效贷款次数比    0\n",
       "工作类型              0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 缺失值处理\n",
    "data.isna().sum()   # 没有缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    199717\n",
       "Name: 受否填写身份证, dtype: int64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 衍生字段\n",
    "# 资产成本做分箱\n",
    "data['受否填写身份证'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    164289\n",
       "1     35428\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 失衡数据判断并处理\n",
    "data['是否违约'].value_counts()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2156443827645186"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "35428/164289      #少类大于10%    # 数据没有失衡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "# 多个备选模型比较\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理模块\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data.drop('是否违约',axis=1),data['是否违约'],test_size=0.3,random_state=2022)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test,model,model_name):\n",
    "    print('训练{}'.format(model_name))\n",
    "    clf=model\n",
    "    start = time.time()\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "     #验证模型\n",
    "    print('训练准确率：{:.4f}'.format(clf.score(X_train, y_train)))\n",
    "    predict=clf.predict(X_test)\n",
    "    score = clf.score(X_test, y_test)\n",
    "    precision=precision_score(y_test,predict)\n",
    "    recall=recall_score(y_test,predict)\n",
    "    print('测试准确率：{:.4f}'.format(score))\n",
    "    print('测试精确率：{:.4f}'.format(precision))\n",
    "    print('测试召回率：{:.4f}'.format(recall))\n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "    return clf, score,precision,recall, duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              False\n",
       "已发货款              False\n",
       "资产成本              False\n",
       "贷款与资产比列           False\n",
       "品牌                False\n",
       "骑车销售商             False\n",
       "车厂                False\n",
       "出生日期              False\n",
       "货款日期              False\n",
       "地区                False\n",
       "对接员工编号            False\n",
       "是否填写手机号           False\n",
       "受否填写身份证           False\n",
       "是否出具驾驶证           False\n",
       "是否填写护照            False\n",
       "信用评分              False\n",
       "主账户贷款次数           False\n",
       "主账户有效贷款次数         False\n",
       "主账户中尚未还清有效贷款      False\n",
       "主账户中已批准的贷款        False\n",
       "主账户中已发放贷款         False\n",
       "次账户贷款次数           False\n",
       "次账户有效贷款次数         False\n",
       "次账户中尚未还清有效贷款      False\n",
       "次账户中已批准贷款         False\n",
       "次账户中已发放贷款         False\n",
       "主账户每月还款           False\n",
       "次账户没用还款           False\n",
       "近六个月新贷款次数         False\n",
       "近六个月违约次数          False\n",
       "平均贷款期限            False\n",
       "第一次贷款距今时间         False\n",
       "贷款查询次数            False\n",
       "贷款与资产比            False\n",
       "贷款总次数             False\n",
       "主账户无效贷款次数         False\n",
       "次账户无效贷款次数         False\n",
       "无效贷款总次数           False\n",
       "尚未还清有效贷款总额        False\n",
       "已批准贷款总额           False\n",
       "已发放贷款总额           False\n",
       "每月还款总额            False\n",
       "贷款与已还贷款比列         False\n",
       "主账户还款期数           False\n",
       "次账户还款期数           False\n",
       "贷款与已批准贷款比列        False\n",
       "总贷款次数与总有效贷款次数比    False\n",
       "工作类型              False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.isnull().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.isnull().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              False\n",
       "已发货款              False\n",
       "资产成本              False\n",
       "贷款与资产比列           False\n",
       "品牌                False\n",
       "骑车销售商             False\n",
       "车厂                False\n",
       "出生日期              False\n",
       "货款日期              False\n",
       "地区                False\n",
       "对接员工编号            False\n",
       "是否填写手机号           False\n",
       "受否填写身份证           False\n",
       "是否出具驾驶证           False\n",
       "是否填写护照            False\n",
       "信用评分              False\n",
       "主账户贷款次数           False\n",
       "主账户有效贷款次数         False\n",
       "主账户中尚未还清有效贷款      False\n",
       "主账户中已批准的贷款        False\n",
       "主账户中已发放贷款         False\n",
       "次账户贷款次数           False\n",
       "次账户有效贷款次数         False\n",
       "次账户中尚未还清有效贷款      False\n",
       "次账户中已批准贷款         False\n",
       "次账户中已发放贷款         False\n",
       "主账户每月还款           False\n",
       "次账户没用还款           False\n",
       "近六个月新贷款次数         False\n",
       "近六个月违约次数          False\n",
       "平均贷款期限            False\n",
       "第一次贷款距今时间         False\n",
       "贷款查询次数            False\n",
       "贷款与资产比            False\n",
       "贷款总次数             False\n",
       "主账户无效贷款次数         False\n",
       "次账户无效贷款次数         False\n",
       "无效贷款总次数           False\n",
       "尚未还清有效贷款总额        False\n",
       "已批准贷款总额           False\n",
       "已发放贷款总额           False\n",
       "每月还款总额            False\n",
       "贷款与已还贷款比列         False\n",
       "主账户还款期数           False\n",
       "次账户还款期数           False\n",
       "贷款与已批准贷款比列        False\n",
       "总贷款次数与总有效贷款次数比    False\n",
       "工作类型              False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.isnull().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              False\n",
       "已发货款              False\n",
       "资产成本              False\n",
       "贷款与资产比列           False\n",
       "品牌                False\n",
       "骑车销售商             False\n",
       "车厂                False\n",
       "出生日期              False\n",
       "货款日期              False\n",
       "地区                False\n",
       "对接员工编号            False\n",
       "是否填写手机号           False\n",
       "受否填写身份证           False\n",
       "是否出具驾驶证           False\n",
       "是否填写护照            False\n",
       "信用评分              False\n",
       "主账户贷款次数           False\n",
       "主账户有效贷款次数         False\n",
       "主账户中尚未还清有效贷款      False\n",
       "主账户中已批准的贷款        False\n",
       "主账户中已发放贷款         False\n",
       "次账户贷款次数           False\n",
       "次账户有效贷款次数         False\n",
       "次账户中尚未还清有效贷款      False\n",
       "次账户中已批准贷款         False\n",
       "次账户中已发放贷款         False\n",
       "主账户每月还款           False\n",
       "次账户没用还款           False\n",
       "近六个月新贷款次数         False\n",
       "近六个月违约次数          False\n",
       "平均贷款期限            False\n",
       "第一次贷款距今时间         False\n",
       "贷款查询次数            False\n",
       "贷款与资产比            False\n",
       "贷款总次数             False\n",
       "主账户无效贷款次数         False\n",
       "次账户无效贷款次数         False\n",
       "无效贷款总次数           False\n",
       "尚未还清有效贷款总额        False\n",
       "已批准贷款总额           False\n",
       "已发放贷款总额           False\n",
       "每月还款总额            False\n",
       "贷款与已还贷款比列         False\n",
       "主账户还款期数           False\n",
       "次账户还款期数           False\n",
       "贷款与已批准贷款比列        False\n",
       "总贷款次数与总有效贷款次数比    False\n",
       "工作类型              False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.isnan(X_test).any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Input contains NaN, infinity or a value too large for dtype('float64').",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-90-086c840aa075>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m     clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n\u001b[0;32m     15\u001b[0m                                                         \u001b[0mX_test\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m                                                         model,model_name)\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[0mresult_df\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mmodel_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Accuracy (%)'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0macc\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m     \u001b[0mresult_df\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mmodel_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'precision(%)'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpre\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-86-6d49850faaf2>\u001b[0m in \u001b[0;36mtrain_model\u001b[1;34m(X_train, y_train, X_test, y_test, model, model_name)\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mclf\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mstart\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mclf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m      \u001b[1;31m#验证模型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'训练准确率：{:.4f}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mclf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\Program Files\\Anaconda3\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight)\u001b[0m\n\u001b[0;32m   1512\u001b[0m             \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0m_dtype\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1513\u001b[0m             \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"C\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1514\u001b[1;33m             \u001b[0maccept_large_sparse\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msolver\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m\"liblinear\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"sag\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"saga\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1515\u001b[0m         )\n\u001b[0;32m   1516\u001b[0m         \u001b[0mcheck_classification_targets\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\Program Files\\Anaconda3\\lib\\site-packages\\sklearn\\base.py\u001b[0m in \u001b[0;36m_validate_data\u001b[1;34m(self, X, y, reset, validate_separately, **check_params)\u001b[0m\n\u001b[0;32m    579\u001b[0m                 \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcheck_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mcheck_y_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    580\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 581\u001b[1;33m                 \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcheck_X_y\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mcheck_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    582\u001b[0m             \u001b[0mout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    583\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\Program Files\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36mcheck_X_y\u001b[1;34m(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)\u001b[0m\n\u001b[0;32m    974\u001b[0m         \u001b[0mensure_min_samples\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mensure_min_samples\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    975\u001b[0m         \u001b[0mensure_min_features\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mensure_min_features\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 976\u001b[1;33m         \u001b[0mestimator\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mestimator\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    977\u001b[0m     )\n\u001b[0;32m    978\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\Program Files\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36mcheck_array\u001b[1;34m(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)\u001b[0m\n\u001b[0;32m    798\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    799\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mforce_all_finite\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 800\u001b[1;33m             \u001b[0m_assert_all_finite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mallow_nan\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mforce_all_finite\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"allow-nan\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    801\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    802\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mensure_min_samples\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\Program Files\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36m_assert_all_finite\u001b[1;34m(X, allow_nan, msg_dtype)\u001b[0m\n\u001b[0;32m    114\u001b[0m             raise ValueError(\n\u001b[0;32m    115\u001b[0m                 msg_err.format(\n\u001b[1;32m--> 116\u001b[1;33m                     \u001b[0mtype_err\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg_dtype\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mmsg_dtype\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    117\u001b[0m                 )\n\u001b[0;32m    118\u001b[0m             )\n",
      "\u001b[1;31mValueError\u001b[0m: Input contains NaN, infinity or a value too large for dtype('float64')."
     ]
    }
   ],
   "source": [
    "model_name_param_dict = {    \n",
    "    'LR': (LogisticRegression(penalty =\"l2\")),\n",
    "                             'DT': (DecisionTreeClassifier(max_depth=10,min_samples_split=10)),\n",
    "                             'AdaBoost': (AdaBoostClassifier()),\n",
    "                             'GBDT': (GradientBoostingClassifier()),\n",
    "                             'RF': (RandomForestClassifier()),\n",
    "                             'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "\n",
    "result_df = pd.DataFrame(columns=['Accuracy (%)','precision(%)','recall(%)','Time (s)'],\n",
    "                             index=list(model_name_param_dict.keys()))\n",
    "\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n",
    "                                                        X_test, y_test,\n",
    "                                                        model,model_name)\n",
    "    result_df.loc[model_name, 'Accuracy (%)'] = acc\n",
    "    result_df.loc[model_name, 'precision(%)'] = pre\n",
    "    result_df.loc[model_name, 'recall(%)'] = recall\n",
    "    result_df.loc[model_name, 'Time (s)'] = mean_duration \n",
    "\n",
    "result_df.to_csv(os.path.join('model_comparison.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 利用网格搜索调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#随机森林\n",
    "#多个决策树组成，尽量使多个树之间是有差异的\n",
    "#使多个树之间是有差异 1.样本抽样， 2. 特征数\n",
    "#控制树过大（怕产生过拟合）---预剪枝\n",
    "#1. 最大深度（树的层数，不包含叶子层）\n",
    "#2. 分裂所需的最小样本数\n",
    "#3. 叶节点最小样本数\n",
    "#n_estimators  子模型的数量(树的个数)\n",
    "#max_features  节点分裂时参与判断的最大特征数\n",
    "#max_depth  最大深度\n",
    "#min_samples_split 分裂所需的最小样本数\n",
    "#min_samples_leaf  叶节点最小样本数\n",
    "#bootstrap 是否bootstrap对样本抽样  False：子模型的样本一致，子模型间强相关  True：默认值\n",
    "param_grid = {'n_estimators': [20, 50, 100,300], 'max_features': [10,20,30,40,50,60],\"max_depth\":[4,6,8,10,12],\n",
    "             \"min_samples_split\": [10,20,30,40],\"min_samples_leaf\": [5,10,20,30]},\n",
    "#为模型能正常创建，可以少设置几个参数选项，让其跑通代码\n",
    "param_grid = {'n_estimators': [20], 'max_features': [60],\"max_depth\":[6,8],\n",
    "             \"min_samples_split\": [40],\"min_samples_leaf\": [30]},\n",
    "#4 * 6 * 5 * 4 * 4 * 5 \n",
    "model = RandomForestClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=5, scoring='roc_auc')\n",
    "result = grid_search.fit(X_train, y_train)\n",
    "result.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#XGBoost(提升树) 弱学习器： cart树（分类回归树）\n",
    "#串行的（第二棵树是在第一棵树的基础上训练，训练的是第一棵树差值）\n",
    "#XGBoost \n",
    "#1. 损失函数\n",
    "#2. 一阶导数g, 二阶导数h (确定叶子节点权重，分支节点的特征)\n",
    "#3. 输出就是 y1 + y2 + y3 \n",
    "#调参\n",
    "#1 剪枝： 最大深度，分裂所需的最小样本数，叶节点最小样本数\n",
    "#2 损失函数\n",
    "#3 棵树\n",
    "#4 数据输入： 记录数据占全部训练集的比例，特征占全部特征的比例\n",
    "#n_estimatores 即决策树的个数\n",
    "#max_depth 树的深度，默认值为6，典型值3-10。\n",
    "#subsample 训练每棵树时，使用的数据占全部训练集的比例。默认值为1，典型值为0.5-1。\n",
    "#colsample_bytree 训练每棵树时，使用的特征占全部特征的比例。默认值为1，典型值为0.5-1。\n",
    "#objective 选定损失函数\n",
    "param_grid = {'n_estimators': [20, 50, 100,300],\"max_depth\":[4,6,8,10,12],\n",
    "             \"subsample\": [0.3,0.5,0.6,0.7,0.8],\"colsample_bytree\": [0.3,0.5,0.6,0.7,0.8]},\n",
    "model = XGBClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=3, scoring='roc_auc')\n",
    "temp=grid_search.fit(X_train, y_train)\n",
    "temp.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存模型\n",
    "from sklearn.externals import joblib\n",
    "#保存模型\n",
    "joblib.dump(temp,'model.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
